package edu.uic.keyoutcomestrackertool;

import java.util.List;

import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Instructor;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Instructors extends ListActivity {

	private static final String LOGTAG = "KOTT";

	private static final int REQUEST_CODE = 100;

	KottDataSource datasource;

	List<Instructor> allInstructorsTasList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_instructors);

		datasource = new KottDataSource(this);

		Bundle extras = getIntent().getExtras();

		if (extras != null){
			if("update".equalsIgnoreCase(extras.getString("option"))){
				Instructor instructor = new Instructor();
				instructor.setInstructorType(extras.getString("type"));
				instructor.setInstructorName(extras.getString("name"));
				instructor.setEmailAdd(extras.getString("email"));
				instructor.setPhoneNo(extras.getString("phone"));
				instructor.setOfficeLocation(extras.getString("officeLocation"));			

				Log.i(LOGTAG,"5 Call instructorUpdate()");
				Log.i(LOGTAG,"6 Instructor to be updated is " + String.valueOf(extras.getString("id")));

				instructor.setInstructorId(Long.parseLong(extras.getString("id")));
				datasource.updateInstructor(instructor);


			}
			refreshDisplay();
		}
		refreshDisplay();		
	}

	public void refreshDisplay(){
		allInstructorsTasList = datasource.findAllInstructorsTas();

		ArrayAdapter<Instructor> instructorAdapter = new ArrayAdapter<Instructor>(this, android.R.layout.simple_list_item_1, allInstructorsTasList);
		setListAdapter(instructorAdapter);
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Instructor instructor = allInstructorsTasList.get(position);

		Intent intent = new Intent(this, InstructorDetails.class);
		intent.putExtra("instructorId", String.valueOf(instructor.getInstructorId()));
		startActivityForResult(intent, REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {		
		super.onActivityResult(requestCode, resultCode, intent);

		if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){

			Log.i(LOGTAG,"1 Option is : " + intent.getStringExtra("option"));

			Log.i(LOGTAG,"2 Instructor id is " + String.valueOf(intent.getStringExtra("id")));						

			if("0".equalsIgnoreCase(String.valueOf(intent.getStringExtra("id")))){
				Log.i(LOGTAG,"3 Call createInstructor()");

				Instructor instructor = new Instructor();
				instructor.setInstructorType(intent.getStringExtra("type"));
				instructor.setInstructorName(intent.getStringExtra("name"));
				instructor.setEmailAdd(intent.getStringExtra("email"));
				instructor.setPhoneNo(intent.getStringExtra("phone"));
				instructor.setOfficeLocation(intent.getStringExtra("officeLocation"));

				long instructorId = datasource.createInstructor(instructor);
				Log.i(LOGTAG,"4 Instructor " + instructor.getInstructorName() + " created with id " + instructorId);
			}
			refreshDisplay();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_add, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {	

		Intent intent = new Intent(this, AddEditInstructor.class);
		startActivityForResult(intent, REQUEST_CODE);
		//finish();
		return super.onOptionsItemSelected(item);
	}	
}
